//
// Created by qsy on 2023/8/11.
//
#include "Read_Pin.h"
#include "tim.h"

uint32_t IDR_Data;
uint8_t flag = 0;

//uint32_t data_buf_send[16]={0};        //单片机作为主机时使用
uint32_t lw_PulseCntNum[PULSE_CHN_MAX]={0};  // 脉冲读取数存储数组


uint16_t IDR_Data1;       //上升阶梯数据存储
uint16_t IDR_Data2;       //下降阶梯数据存储

uint8_t Step_R = STEPONE; //上升初始步骤1
uint8_t Step_F = STEPONE; //下降初始步骤1

uint8_t RASING_FALLING = RASING;//上升下降阶梯标志，初始设置为上升阶梯检测

uint32_t ProtectionPar = 0;

//读16个引脚，作10判断，出现10后break，计算加1
void Read_Port(uint32_t * data_buf_p,uint16_t IDR_Data)
{
    uint8_t i = 0;
    while(1)
    {
        if (((IDR_Data >> i) & 1)&&((IDR_Data >> i+1) & 1))
        {
            i++;
            if (i == 15)
            {
                data_buf_p[i]++;
                break;
            }
        }
        else
        {
            data_buf_p[i]++;
            break;
        }
    }
}


//用16次判读来处理数据，在此便于修改
void read_port(uint32_t * data_buf_p)
{
    IDR_Data = (uint16_t) GPIOB->IDR;
    if (IDR_Data == 0x01)
    {
        data_buf_p[0]++;
    }
    else if (IDR_Data == 0x03)
    {
        data_buf_p[1]++;
    }
    else if (IDR_Data == 0x07)
    {
        data_buf_p[2]++;
    }
    else if (IDR_Data == 0x0F)
    {
        data_buf_p[3]++;
    }
    else if (IDR_Data == 0x1F)
    {
        data_buf_p[4]++;
    }
    else if (IDR_Data == 0x3F)
    {
        data_buf_p[5]++;
    }
    else if (IDR_Data == 0x7F)
    {
        data_buf_p[6]++;
    }
    else if (IDR_Data == 0xFF)
    {
        data_buf_p[7]++;
    }
    else if (IDR_Data == 0x1FF)
    {
        data_buf_p[8]++;
    }
    else if (IDR_Data == 0x3FF)
    {
        data_buf_p[9]++;
    }
    else if (IDR_Data == 0x7FF)
    {
        data_buf_p[10]++;
    }
    else if (IDR_Data == 0xFFF)
    {
        data_buf_p[11]++;
    }
    else if (IDR_Data == 0x1FFF)
    {
        data_buf_p[12]++;
    }
    else if (IDR_Data == 0x3FFF)
    {
        data_buf_p[13]++;
    }
    else if (IDR_Data == 0x7FFF)
    {
        data_buf_p[14]++;
    }
    else if (IDR_Data == 0xFFFF)
    {
        data_buf_p[15]++;
    } else{

    }

}
//PB0上升下降沿中断函数
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
    IDR_Data =(uint16_t) GPIOB->IDR;
    //上升沿触发
    if ((IDR_Data >> 0) & 1)
    {
        HAL_TIM_Base_Start_IT(&htim4); //开启定时器4 微妙计数
    }
    //下降沿触发
    else{
        HAL_TIM_Base_Stop_IT(&htim4);     //关闭定时4
        ProtectionPar = 0;
    }
}

//#define RASING  1         //检测阶梯上升标志
//#define FALLING 0         //检测下降阶梯标志
//#define STEPONE 1         //步骤1标志
//#define STEPONE 2         //步骤2标志


//在PB0上升沿开启中断时进入，定时器溢出中断在回调函数中运行，PB0下降沿中断时退出
void HandelData(uint32_t * data_buf_p)
{
    //0.08/(0.25*10^(-6)) = 320000 中断开闭保护 80ms 后主动关闭定时器 定时器改成2时 要乘4.5
    ProtectionPar ++;
    if (ProtectionPar == 320000){
        ProtectionPar = 0;
        Step_R = STEPONE; //上升初始步骤1
        Step_F = STEPONE; //下降初始步骤1
        RASING_FALLING = RASING;//上升下降阶梯标志，初始设置为上升阶梯检测
        HAL_TIM_Base_Stop_IT(&htim4);     //关闭定时4
    }

    //上升阶梯检测
    if (RASING_FALLING == RASING)
    {
        //步骤1 获取初始数值后进入步骤2
        if (Step_R == STEPONE)
        {
            IDR_Data1 = (uint16_t) GPIOB->IDR;
            Step_R = STEPONE;
        }
        //步骤2
        else
        {
            //如果检测到了上升阶梯，开启下降阶梯检测，进入步骤1
            if (IDR_Data1 < (uint16_t) GPIOB->IDR)
            {
                RASING_FALLING = FALLING;
                Step_R = STEPONE;
            }
            else
            {
                //如果没有上升阶梯，替换上一个数值
                IDR_Data1 = (uint16_t) GPIOB->IDR;
            }
        }
    }
    //下降阶梯检测
    else
    {
        //步骤1 获取初始数值后进入步骤2
        if (Step_F == STEPONE)
        {
            IDR_Data2 = (uint16_t) GPIOB->IDR;
            Step_F = STEPONE;
        }
        //步骤2
        else
        {
            //如果检测到了下降阶梯，处理最大的IDR_Data数值，在最高位进行累加
            if (IDR_Data2 > (uint16_t) GPIOB->IDR)
            {
                Read_Port(data_buf_p,IDR_Data2);
                Step_F = STEPONE;
                RASING_FALLING = RASING;
            }
            else
            {
                //没有下降阶梯，替换上一个数值
                IDR_Data2 = (uint16_t) GPIOB->IDR;
            }
        }
    }
}














